package com.dayu.recovery.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.dayu.common.core.domain.entity.SysRole;
import com.dayu.common.utils.DateUtils;
import com.dayu.common.utils.SecurityUtils;
import com.dayu.recovery.domain.RecoveryGoods;
import com.dayu.recovery.domain.TransferGoods;
import com.dayu.recovery.mapper.RecoveryGoodsMapper;
import com.dayu.recovery.mapper.TransferGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayu.recovery.mapper.WarehouseLogsMapper;
import com.dayu.recovery.domain.WarehouseLogs;
import com.dayu.recovery.service.IWarehouseLogsService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 出入库日志Service业务层处理
 * 
 * @author HK
 * @date 2023-12-17
 */
@Service
public class WarehouseLogsServiceImpl implements IWarehouseLogsService 
{
    @Resource
    private WarehouseLogsMapper warehouseLogsMapper;

    @Resource
    private TransferGoodsMapper transferGoodsMapper;

    @Resource
    private RecoveryGoodsMapper recoveryGoodsMapper;


    /**
     * 查询出入库日志
     * 
     * @param id 出入库日志ID
     * @return 出入库日志
     */
    @Override
    public WarehouseLogs selectWarehouseLogsById(Long id)
    {
        WarehouseLogs logs = warehouseLogsMapper.selectWarehouseLogsById(id);
        TransferGoods goods = new TransferGoods();
        goods.setWarehouseLogsId(logs.getId());
        logs.setGoodsList(transferGoodsMapper.selectTransferGoodsList(goods));
        System.out.println("当前列表：");
        System.out.println(logs.getGoodsList());
        return logs;
    }

    @Override
    public List<TransferGoods>  queryGoodsById(Long logsId) {
        WarehouseLogs logs = selectWarehouseLogsById(logsId);
        List<TransferGoods> goods = logs.getGoodsList();
        goods.forEach(item -> item.setNewStockName(logs.getInStockInfo()));
        return goods;
    }

    /**
     * 查询出入库日志列表
     * 
     * @param warehouseLogs 出入库日志
     * @return 出入库日志
     */
    @Override
    public List<WarehouseLogs> selectWarehouseLogsList(WarehouseLogs warehouseLogs)
    {
        //获取当前角色
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        boolean bool = false;
        for (SysRole r:  roles) {
            if(r.getRoleKey().equals("sales")) {
                bool = true;
                break;
            }
        }
        if(bool) {
            warehouseLogs.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        }
        return warehouseLogsMapper.selectWarehouseLogsList(warehouseLogs);
    }

    /**
     * 新增出入库日志
     * 
     * @param warehouseLogs 出入库日志
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWarehouseLogs(WarehouseLogs warehouseLogs)
    {
        warehouseLogs.setCreateBy(SecurityUtils.getUsername());
        warehouseLogs.setCreateTime(DateUtils.getNowDate());
        warehouseLogs.setStatus(1);
        warehouseLogs.setNumber(warehouseLogs.getGoodsList().size());
        int flag = warehouseLogsMapper.insertWarehouseLogs(warehouseLogs);
        if(flag > 0) {
            saveTransferGoodsList(warehouseLogs);
        }
        return flag;
    }

    /**
     * 修改出入库日志
     * 
     * @param warehouseLogs 出入库日志
     * @return 结果
     */
    @Override
    @Transactional
    public int updateWarehouseLogs(WarehouseLogs warehouseLogs)
    {
        WarehouseLogs logs = warehouseLogsMapper.selectWarehouseLogsById(warehouseLogs.getId());
        if(ObjectUtils.isEmpty(logs))
            throw new RuntimeException("没有获取到数据！");
        if(logs.getStatus() != 1)
            throw new RuntimeException("不可更改！");
        warehouseLogs.setUpdateTime(DateUtils.getNowDate());
        saveTransferGoodsList(warehouseLogs);
        warehouseLogs.setNumber(warehouseLogs.getGoodsList().size());
        return warehouseLogsMapper.updateWarehouseLogs(warehouseLogs);
    }

    private void saveTransferGoodsList(WarehouseLogs bo) {
        if(ObjectUtils.isEmpty(bo.getGoodsList()) || bo.getGoodsList().isEmpty())
            return;
        List<TransferGoods> addGoods = new ArrayList<>();
        List<RecoveryGoods> updateGoods = new ArrayList<>();
        bo.getGoodsList().forEach(it -> {
            if(ObjectUtils.isEmpty(it.getId())) {
                it.setWarehouseLogsId(bo.getId());
                addGoods.add(it);
                RecoveryGoods goods = new RecoveryGoods();
                goods.setGoodsId(it.getGoodsId());
                goods.setConfirmStatus(2);
                updateGoods.add(goods);
            }
        });

        if(!addGoods.isEmpty())
            transferGoodsMapper.insertBatchByList(addGoods);
        if(!updateGoods.isEmpty())
            recoveryGoodsMapper.updateGoodsConfirmStatusByList(updateGoods);
    }

    @Override
    @Transactional
    public int confirm(Long id) {
        WarehouseLogs logs = verity(id);
        List<String> goodsIds = new ArrayList<>();
        logs.getGoodsList().forEach(it -> {
            goodsIds.add(it.getGoodsId());
        });
        if(!goodsIds.isEmpty())
            recoveryGoodsMapper.updateGoodsStockIdAndConfirmStatus(goodsIds, logs.getInStockId());
        logs.setStatus(2);
        logs.setUpdateBy(SecurityUtils.getUsername());
        logs.setUpdateTime(new Date());
        return warehouseLogsMapper.updateWarehouseLogs(logs);
    }

    private WarehouseLogs verity(Long id) {
        WarehouseLogs logs = selectWarehouseLogsById(id);
        if(ObjectUtils.isEmpty(logs))
            throw new RuntimeException("没有获取到参数！");
        if(logs.getStatus() != 1)
            throw new RuntimeException("不可操作！");
        return logs;
    }

    @Override
    @Transactional
    public int cancel(Long id) {
        WarehouseLogs logs = verity(id);
        List<RecoveryGoods> goodsList = new ArrayList<>();
        logs.getGoodsList().forEach(it -> {
            RecoveryGoods goods = new RecoveryGoods();
            goods.setGoodsId(it.getGoodsId());
            goods.setConfirmStatus(1);
            goodsList.add(goods);
        });

        if(!goodsList.isEmpty())
            recoveryGoodsMapper.updateGoodsConfirmStatusByList(goodsList);

        logs.setStatus(3);
        logs.setUpdateBy(SecurityUtils.getUsername());
        logs.setUpdateTime(new Date());
        return warehouseLogsMapper.updateWarehouseLogs(logs);
    }

    @Override
    public List<Map<String, Object>> queryGoodsDetailLogList(String goodsId) {
        return warehouseLogsMapper.queryGoodsDetailLogList(goodsId);
    }
}
